package models

import (
	"gorm.io/gorm"
	"zk3/common/mysql"
)

type User struct {
	gorm.Model
	Mobile string `gorm:"type:char(11);not null;comment:手机号"`
	Code   string `gorm:"type:char(32);not null;comment:验证码"`
}

type UserWork struct {
	gorm.Model
	UserId      int32  `gorm:"type:int(10);not null;index;comment:用户ID"`
	Author      string `gorm:"type:varchar(30);not null;comment:作者"`
	ReleaseTime string `gorm:"type:varchar(20);not null;comment:发布时间"`
}

func (u *UserWork) UserWorkCreate() error {
	return mysql.Db.Model(UserWork{}).Create(&u).Error
}

type UserGive struct {
	gorm.Model
	UserId    int32 `gorm:"type:int(10);not null;index;comment:用户ID"`
	LikeCount int32 `gorm:"type:int(10);not null;comment:点赞量"`
}

func (u *UserGive) UserGiveCreate() error {
	return mysql.Db.Model(UserGive{}).Create(&u).Error
}

func UserGiveDel(likeCount int32) (u *User, err error) {
	err = mysql.Db.Model(UserGive{}).Delete(likeCount).Error
	return
}

type UserComment struct {
	gorm.Model
	UserId  int32  `gorm:"type:int(10);not null;index;comment:用户ID"`
	Author  string `gorm:"type:varchar(30);not null;comment:作者"`
	Content string `gorm:"type:varchar(50);not null;comment:内容"`
}

func (u *UserComment) UserCommentCreate() error {
	return mysql.Db.Model(UserComment{}).Create(&u).Error
}
func (u *User) Reg() error {
	return mysql.Db.Model(User{}).Create(&u).Error
}

func Login(mobile string) (u *User, err error) {
	err = mysql.Db.Model(User{}).Where("mobile = ?", mobile).First(&u).Error
	return
}

func List(mobile string) (u *User, err error) {
	err = mysql.Db.Model(User{}).Where("mobile = ?", mobile).First(&u).Error
	return
}
